﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuanAnhPersonalWeb.Models;


namespace TuanAnhPersonalWeb.Classes
{
    public class ContentAccessConsolidator
    {
        TuanAnhEntities db = new TuanAnhEntities();
        public IEnumerable<TUANANH__TYPE> getAllType()
        {
            var data = db.TUANANH__TYPE.Select(e => e).OrderByDescending(e=>e.TYPE_ID).ToList();
            return data.Count() > 0 ? data : null;
        }
        public IList<ContentModel> getallContent()
        {
            IList<ContentModel> list = new List<ContentModel>();
            var contentList = db.TUANANH__CONTENT.ToList();
            foreach (var item in contentList)
            {
                ContentModel model = new ContentModel(); 
                
                model.CONTENT_CONTENT = item.CONTENT_CONTENT;
                model.CONTENT_ID = item.CONTENT_ID;
                model.CONTENT_TITLE = item.CONTENT_TITLE;
                model.CREATEDATE = item.CREATEDATE;
                model.IMAGE_DIR = item.IMAGE_DIR;
                model.ISHOMEPAGE = item.ISHOMEPAGE;
                model.LINK = item.LINK;
                model.SHORT_CONTENT = item.SHORT_CONTENT;
                model.TYPE_ID=item.TYPE_ID;
                model.TYPE = db.TUANANH__TYPE.Where(e => e.TYPE_ID == item.TYPE_ID).Select(e => e.DESCRIPTION).Single();
                list.Add(model);
            }
            return list;
        }

        public ContentModel getcontetByID(int id)
        {
            TUANANH__CONTENT item = db.TUANANH__CONTENT.Single(e => e.CONTENT_ID == id);
            ContentModel model = new ContentModel();
            model.CONTENT_CONTENT = item.CONTENT_CONTENT;
            model.CONTENT_ID = item.CONTENT_ID;
            model.CONTENT_TITLE = item.CONTENT_TITLE;
            model.CREATEDATE = item.CREATEDATE;
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.ISHOMEPAGE = item.ISHOMEPAGE;
            model.LINK = item.LINK;
            model.SHORT_CONTENT = item.SHORT_CONTENT;
            model.TYPE = db.TUANANH__TYPE.Where(e => e.TYPE_ID == item.TYPE_ID).Select(e => e.DESCRIPTION).Single();
            model.TYPE_ID = item.TYPE_ID;
            return model;

        }
        public void editImage(ContentModel item)
        {
            TUANANH__CONTENT model = db.TUANANH__CONTENT.Single(e => e.CONTENT_ID == item.CONTENT_ID);
            model.CONTENT_CONTENT = item.CONTENT_CONTENT;
            model.CONTENT_TITLE = item.CONTENT_TITLE;
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.TYPE_ID = item.TYPE_ID;
            model.SHORT_CONTENT = item.SHORT_CONTENT;
            db.SaveChanges();
        }
        public void updateCode()
        {
            int? Code = db.TUANANH__CODE.Where(e => e.TYPE.Equals("CODE")).Select(e => e.CODE).Single();
            TUANANH__CODE model = db.TUANANH__CODE.Single(e => e.CODE == Code);
            model.CODE = Code + 1;
            db.SaveChanges();
        }
        public void InsertContent(ContentModel item)
        {
            int? Code = db.TUANANH__CODE.Where(e => e.TYPE.Equals("CODE")).Select(e => e.CODE).Single();
            int key = int.Parse(Code.ToString()) + 1;
            TUANANH__CONTENT model = new TUANANH__CONTENT();
            model.CONTENT_ID = key;
            model.CONTENT_TITLE = item.CONTENT_TITLE;
            model.CONTENT_CONTENT = item.CONTENT_CONTENT;
            model.LINK = "Content/Details/" + key.ToString();
            model.SHORT_CONTENT = item.SHORT_CONTENT;
            model.ISHOMEPAGE = false;
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.CREATEDATE = DateTime.Now;
            model.TYPE_ID = item.TYPE_ID;
            db.TUANANH__CONTENT.AddObject(model);
            db.SaveChanges();
            updateCode();
        }
        public void updateHomeStt()
        {
            var date = db.TUANANH__CONTENT.Where(e=>e.ISHOMEPAGE == true).Min(e => e.CREATEDATE);
            TUANANH__CONTENT home = db.TUANANH__CONTENT.Where(e => e.ISHOMEPAGE == true && e.CREATEDATE==date).Single();
            home.ISHOMEPAGE = false;
            db.SaveChanges();
        }
        public void updateEventToHome(int id)
        {
            updateHomeStt();
            TUANANH__CONTENT model = db.TUANANH__CONTENT.Where(e => e.CONTENT_ID == id).Single();
            model.ISHOMEPAGE = true;
            db.SaveChanges();
        }
        public void updateDate(int id)
        {
            TUANANH__CONTENT model = db.TUANANH__CONTENT.Where(e => e.CONTENT_ID == id).Single();
            model.CREATEDATE = DateTime.Now;
            db.SaveChanges();
        }
    }
}